import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.opencv_objdetect;
import org.bytedeco.javacv.*;
import org.bytedeco.javacpp.*;
import util.JavaCVUtil;

import javax.swing.*;
import java.nio.DoubleBuffer;
import java.nio.IntBuffer;

/**
 * Created by Administrator on 2017/10/13.
 */
public class javaCvTest {
    public static class FaceDetection {
        private FaceDetection myFaceDetection;
        private opencv_objdetect.CascadeClassifier faceDetector = new opencv_objdetect.CascadeClassifier(getClass()
                .getResource("/haarcascade_frontalface_default.xml").getPath());

        public FaceDetection() throws FrameGrabber.Exception, InterruptedException {

////        CascadeClassifier faceDetector = new CascadeClassifier(getClass()
////                .getResource("/lbpcascade_frontalface.xml").getPath());
////        faceDetector.load("E:\\workwrok\\tencetFace\\src\\main\\resources\\lbpcascade_frontalface.xml");
//
//            opencv_objdetect.CascadeClassifier faceDetector = new opencv_objdetect.CascadeClassifier(getClass()
//                    .getResource("/haarcascade_frontalface_default.xml").getPath());
//            faceDetector.load("E:\\workwrok\\tencetFace\\src\\main\\resources\\haarcascade_frontalface_default.xml");
//
////        Mat image = Imgcodecs.imread(getClass().getResource(
////                "/AverageMaleFace.jpg").getPath());
////            opencv_core.Mat image = Imgcodecs.imread("C:\\Users\\Administrator\\Desktop\\20171013\\4.png");
//            opencv_core.Mat image = JavaCVUtil.imRead("C:\\Users\\Administrator\\Desktop\\20171013\\6.png");
//            // Detect faces in the image.
//            // MatOfRect is a special container class for Rect.
////            MatOfRect faceDetections = new MatOfRect();
//            opencv_core.RectVector rectVector = new opencv_core.RectVector();
//            faceDetector.detectMultiScale(image, rectVector);
//
//            System.out.println(String.format("Detected %s faces",
//                    rectVector.size()));
//
//            for (int i = 0; i < rectVector.size(); i++) {
//                opencv_core.Rect rect = rectVector.get(i);
////                Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x
////                        + rect.width, rect.y + rect.height), new opencv_core.Scalar(0, 255, 0));
//                opencv_imgproc.rectangle(image, new opencv_core.Point(rect.x(), rect.y()), new opencv_core.Point(rect.x()
//                        + rect.width(), rect.y() + rect.height()), new opencv_core.Scalar(0, 255, 0, 0));
//            }
//            // Draw a bounding box around each face.
////            for (Rect rect : rectVector.toArray()) {
////                Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x
////                        + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
////            }
//
//            // Save the visualized detection.
//            String filename = "C:\\Users\\Administrator\\Desktop\\20171013\\dec.jpg";
//            System.out.println(String.format("Writing %s", filename));
//            JavaCVUtil.imWrite(image, filename);


            faceDetector.load("E:\\workwrok\\tencetFace\\src\\main\\resources\\haarcascade_frontalface_default.xml");
            FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("C:\\Users\\Administrator\\Desktop\\20171013\\N04141517.mp4");
            grabber.start();
            CanvasFrame canvas = new CanvasFrame("Video");
            canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            canvas.setAlwaysOnTop(true);

//            opencv_videoio.VideoCapture video = new opencv_videoio.VideoCapture();
//            String address = "192.168.20.235:9101";
//            String address1 = "http://192.168.20.235:9101/?action=stream.mjpg";
//            String rtsp_addr = "rtsp://abc:123@192.168.20.235:9101/MPEG-4/ch1/main/av_stream";
//            if (video.open(rtsp_addr)) {
//                if(!canvas.isDisplayable()) {
//                }
//            }


            while (true) {
                if (!canvas.isDisplayable()) {
                    grabber.stop();
                    System.exit(2);
                }
                Frame showFrame = grabber.grab();
                if (showFrame.image != null) {
                    showFrame = detect(showFrame);
                }

                canvas.showImage(showFrame);

                Thread.sleep(10);
            }
        }

        public Frame detect(Frame img) {
            OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
            opencv_core.Mat imgage = converter.convertToMat(img);

            opencv_core.RectVector rectVector = new opencv_core.RectVector();
//            faceDetector.detectMultiScale(imgage, rectVector);
            IntBuffer a = IntBuffer.allocate(1);
            DoubleBuffer b =DoubleBuffer.allocate(1);
            double c = 1.1;
            opencv_core.Size d = new opencv_core.Size();
            opencv_core.Size e = new opencv_core.Size();
            faceDetector.detectMultiScale3(imgage, rectVector, a, b);

            for (int i = 0; i < rectVector.size(); i++) {
                opencv_core.Rect rect = rectVector.get(i);
                opencv_imgproc.rectangle(imgage, new opencv_core.Point(rect.x(), rect.y()), new opencv_core.Point(rect.x()
                        + rect.width(), rect.y() + rect.height()), new opencv_core.Scalar(0, 255, 0, 0));
            }

            Frame convertFrame1 = converter.convert(imgage);

            return convertFrame1;

        }
    }

    public static void main(String args[]) throws FrameGrabber.Exception, InterruptedException {

//        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        FaceDetection faceDetection = new FaceDetection();
    }
}
